#include "mondialoguefiltrepersonnalise.h"
#include "ui_mondialoguefiltrepersonnalise.h"
#include <QSpinBox>

MonDialogueFiltrePersonnalise::MonDialogueFiltrePersonnalise(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MonDialogueFiltrePersonnalise)
{
    ui->setupUi(this);

    this->rayonH    = 1;
    this->rayonV    = 1;
    this->majAffichage();

    QObject::connect(ui->horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(majHorizontal(int)));
    QObject::connect(ui->verticalSlider, SIGNAL(valueChanged(int)), this, SLOT(majVertical(int)));
}

MonDialogueFiltrePersonnalise::~MonDialogueFiltrePersonnalise()
{
    delete ui;
}


void MonDialogueFiltrePersonnalise::majHorizontal(int rayon){
    this->viderLayout();
    this->rayonH    = rayon;
    this->majAffichage();
}

void MonDialogueFiltrePersonnalise::majVertical(int rayon){
    this->viderLayout();
    this->rayonV    = rayon;
    this->majAffichage();
}

void MonDialogueFiltrePersonnalise::viderLayout(){
    QLayoutItem * item;
    for(int i=0; i<2*this->rayonV+1; i++){
        for(int j=0; j<2*this->rayonH+1; j++){
            item    = ui->gridLayout->itemAtPosition(i, j);
            ui->gridLayout->removeItem(item);
            delete item->widget();
        }
    }
    this->ui->gridLayoutWidget->repaint();
}

void MonDialogueFiltrePersonnalise::majAffichage(){
    QSpinBox *spin;
    for(int i=0; i<2*this->rayonV+1; i++){
        for(int j=0; j<2*this->rayonH+1; j++){
            spin    = new QSpinBox(this);
            spin->setValue(1);
            spin->setMaximum(30);
            spin->setMinimum(-30);
            ui->gridLayout->addWidget(spin, i, j);
        }
    }
}

Matrices * MonDialogueFiltrePersonnalise::getNoyauPerso(){
    Matrices * output   = new Matrices(2*this->rayonV +1, 2*this->rayonH +1);

    QSpinBox * spin;

    for(int i=0; i<2*this->rayonV+1; i++){
        for(int j=0; j<2*this->rayonH+1; j++){
            spin    = (QSpinBox *)(this->ui->gridLayout->itemAtPosition(i, j)->widget());
            output->setValeurDansCase(i, j, spin->value());
        }
    }

    return output;
}
